package com.fare.taluo.application.impl.analyse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.Filter;
import org.foreveross.hbase.client.HBaseHelper;
import org.foreveross.hbase.client.HBasePlaneinfoHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.fare.taluo.GlobalsConst;
import com.fare.taluo.application.FreightPriceApplication;
import com.fare.taluo.application.analyse.ChannelContrastApplication;
import com.fare.taluo.application.impl.FreightPriceApplicationImpl;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.PlaneInfo;
/**
 * 官网渠道对比
 * @author spring.华 
 */
@Scope("singleton")
@Service("channelContrastApplication")
public class ChannelContrastApplicationImpl implements ChannelContrastApplication{
	/*@Resource(name="freightPriceApplication")
	private FreightPriceApplication freightPriceApplication;*/
	public List<PlaneInfo> earchChannelContrast(String tableName,String startRowKey,String endRowKey,List<Filter> filters) throws IOException{
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
		
		
		//航班时间yyyymmddhh 10位
		if(startRowKey.contains("/") || startRowKey.contains("null")){
			return list;
		}
		
		//走势暂时注释
		//String yesterdayStartRowKey = this.getYesterdayRowKey(startRowKey);
		//String yesterdayEndRowKey = this.getYesterdayRowKey(endRowKey);
		FreightPriceApplication freightPriceApplication = new FreightPriceApplicationImpl();
		
		//List<PlaneInfo> yesterdayList = freightPriceApplication.findByConditionsToPlaneInfo(tableName, yesterdayStartRowKey, yesterdayEndRowKey, filters);
		//List<PlaneInfo> yesterdayList = null;
		list = freightPriceApplication.findByConditionsToPlaneInfo(tableName, startRowKey, endRowKey, filters);
		/*if(yesterdayList != null && yesterdayList.size()>0){
			for (PlaneInfo planeInfo : list) {
				String currentHour = planeInfo.getFlightBatchStartTime().substring(8, 10);	
				String currentFlightNo = planeInfo.getFlightId();
				double cPrice = Double.parseDouble(planeInfo.getLowerPrice());
				boolean isFlightNoEqual = false;
				for(PlaneInfo yesterdayPlane : yesterdayList){
					double yPrice = Double.parseDouble(yesterdayPlane.getLowerPrice());//昨天最低价
					String yflightHour= yesterdayPlane.getFlightBatchStartTime().substring(8, 10);//20130722150000->2013072215
					String yFlightNo = yesterdayPlane.getFlightId();
					if(yFlightNo.equals(currentFlightNo) && yflightHour.equals(currentHour)){
						isFlightNoEqual = true;
						if(cPrice < yPrice){
							//小于前一天,下降
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_DESCEND);
						}else if(cPrice > yPrice){
							//大于前一天,上升
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_ASCEND);
						}else {
							//等于前一天,持平
							planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
						}
					}
						
				}
				if(isFlightNoEqual==false){
					planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
				}
				
				
				if( yFlightNo.equals(currentFlightNo) && yflightHour.equals(currentHour)){
					double cPrice = Double.parseDouble(planeInfo.getLowerPrice());
					if(cPrice < yPrice){
						//小于前一天,下降
						planeInfo.setTendency(GlobalsConst.PRICE_STATUS_DESCEND);
					}else if(cPrice > yPrice){
						//大于前一天,上升
						planeInfo.setTendency(GlobalsConst.PRICE_STATUS_ASCEND);
					}else {
						//等于前一天,持平
						planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
					}
				}
				
			}
		}else{
			//没有数据运价走势持平
 			
				for (PlaneInfo planeInfo : list) {
				planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
			}
		}*/
		/*if(yesterdayList != null && yesterdayList.size()>0){
 			for (Iterator iterator = yesterdayList.iterator(); iterator
					.hasNext();) {
				PlaneInfo yesterdayPlane = (PlaneInfo) iterator.next();
				double yPrice = Double.parseDouble(yesterdayPlane.getLowerPrice());//昨天最低价
				String yflightHour= yesterdayPlane.getFlightBatchStartTime().substring(8, 10);//20130722150000->2013072215
				String yFlightNo = yesterdayPlane.getFlightId();
				if(list != null && list.size()>0){
					for (PlaneInfo planeInfo : list) {
						String currentHour = planeInfo.getFlightBatchStartTime().substring(8, 10);	
						String currentFlightNo = planeInfo.getFlightId();
						if( yFlightNo.equals(currentFlightNo) && yflightHour.equals(currentHour)){
							double cPrice = Double.parseDouble(planeInfo.getLowerPrice());
							if(cPrice < yPrice){
								//小于前一天,下降
								planeInfo.setTendency(GlobalsConst.PRICE_STATUS_DESCEND);
							}else if(cPrice > yPrice){
								//大于前一天,上升
								planeInfo.setTendency(GlobalsConst.PRICE_STATUS_ASCEND);
							}else {
								//等于前一天,持平
								planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
							}
						}
						
					}
				}
					
			}
 		}else{//没有数据运价走势持平
 			
 				for (PlaneInfo planeInfo : list) {
					planeInfo.setTendency(GlobalsConst.PRICE_STATUS_MAINTAIN);
				}
 				
 			
 		}*/
		
		
		
		
		return list;
		
	}
	public List<PlaneInfo> searchByHbaseRowModel(String tableName,String startRowKey,String endRowKey,List<Filter> filters){
		
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)
		List<PlaneInfo> list = null;
		try {
			list =  this.earchChannelContrast(tableName,startRowKey, endRowKey, filters);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
		
	}
	public List<PlaneInfo> earchChannelContrast(String startRowKey,String endRowKey,List<Filter> filters) throws IOException{
		List<PlaneInfo> list=new ArrayList<PlaneInfo> ();
		//航班最低价LP_FLIGHT：  渠道_起点(使用三字码)_终点(使用三字码)_(航班号)_航班日期(年月日时)
		//LP_LINE  渠道_起点(使用三字码)_终点(使用三字码)_航班日期(年月日时)

		ResultScanner scan=HBaseHelper.getRecords("LP_LINE_HOUR", startRowKey, endRowKey,null,filters);
		list=HBasePlaneinfoHelper.transformHBaseResultToPlaneinfo(scan);
		return list;
		
	}
	
	private String getYesterdayRowKey(String rowKey){
		String flightStr = null;
		String yesterDate = null;
		if(rowKey.length()==29){
			flightStr = rowKey.substring(19,27);
		}else{
			flightStr = rowKey.substring(11,19);
		}
		
		Calendar cal = Calendar.getInstance();
		Date flightDate = DateUtils.StringToDate("yyyyMMdd", flightStr);
		cal.setTime(flightDate);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		yesterDate = DateUtils.formatDay(cal.getTime(), "yyyyMMdd");
		return rowKey.substring(0, 19)+yesterDate.substring(0,8)+(rowKey.length()==29?rowKey.substring(27):rowKey.substring(19));
	}
	
	public List<PlaneInfo> getPlaneInfos(List<String> dates, String rowKeyHead,
			String checkBox_str, String timeBegin, String timeEnd)
			throws Exception {
		//单选
		return this.getPlaneInfos(dates, rowKeyHead, ChoseEnum.valueOf(checkBox_str), timeBegin, timeEnd);
	}
	///
	public List<PlaneInfo> getPlaneInfos(String tablename,List<String> dates, String rowKeyHead,
			String checkBox_str, String timeBegin, String timeEnd)
			throws Exception {
		//单选
		return this.getPlaneInfos(tablename,dates, rowKeyHead, ChoseEnum.valueOf(checkBox_str), timeBegin, timeEnd);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<PlaneInfo> getPlaneInfos(List<String> dates,String rowKeyHead,String str)throws Exception{
		if(str.length()==1){//单选
			return this.getPlaneInfos(dates, rowKeyHead, ChoseEnum.valueOf(str));
		}else{//组合
			List<List<PlaneInfo>> lists=new ArrayList();
			for (int i = 0; i < str.length(); i++) {
				lists.add(this.getPlaneInfos(dates, rowKeyHead, ChoseEnum.valueOf(str.charAt(i)+"")));
			}
			List<PlaneInfo> listInfo=new ArrayList();
			for (Iterator iterator = lists.iterator(); iterator.hasNext();) {
				List<PlaneInfo> list = (List<PlaneInfo>) iterator.next();
				for (Iterator iterator2 = list.iterator(); iterator2.hasNext();) {
					PlaneInfo planeInfo = (PlaneInfo) iterator2.next();
					if(planeInfo!=null){
						listInfo.add(planeInfo);
					}
				}
			}
			return listInfo;
		}
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<PlaneInfo> getPlaneInfos(String tablename,List<String> dates,String rowKeyHead,String str)throws Exception{
		if(str.length()==1){//单选
			return this.getPlaneInfos(tablename,dates, rowKeyHead, ChoseEnum.valueOf(str));
		}else{//组合
			List<List<PlaneInfo>> lists=new ArrayList();
			for (int i = 0; i < str.length(); i++) {
				lists.add(this.getPlaneInfos(tablename,dates, rowKeyHead, ChoseEnum.valueOf(str.charAt(i)+"")));
			}
			List<PlaneInfo> listInfo=new ArrayList();
			for (Iterator iterator = lists.iterator(); iterator.hasNext();) {
				List<PlaneInfo> list = (List<PlaneInfo>) iterator.next();
				for (Iterator iterator2 = list.iterator(); iterator2.hasNext();) {
					PlaneInfo planeInfo = (PlaneInfo) iterator2.next();
					if(planeInfo!=null){
						listInfo.add(planeInfo);
					}
				}
			}
			return listInfo;
		}
	}
	
	
	private List<PlaneInfo> getPlaneInfos(List<String> dates,String rowKeyHead,ChoseEnum choseEnum)throws Exception{
		//单选
		return choseEnum.getPlaneInfos(dates, rowKeyHead);
	}
	//
	private List<PlaneInfo> getPlaneInfos(String tablename,List<String> dates,String rowKeyHead,ChoseEnum choseEnum)throws Exception{
		//单选
		return choseEnum.getPlaneInfos(tablename,dates, rowKeyHead);
	}
	private List<PlaneInfo> getPlaneInfos(List<String> dates,String rowKeyHead,ChoseEnum choseEnum,String timeBegin, String timeEnd)throws Exception{
			//自定义时间单选
			return choseEnum.getPlaneInfos(dates, rowKeyHead,timeBegin,timeEnd);
	}
	///
	private List<PlaneInfo> getPlaneInfos(String tablename,List<String> dates,String rowKeyHead,ChoseEnum choseEnum,String timeBegin, String timeEnd)throws Exception{
		//自定义时间单选
		return choseEnum.getPlaneInfos(tablename,dates, rowKeyHead,timeBegin,timeEnd);
	}
	
	
	public static void main(String[] args) throws IOException {
		ChannelContrastApplicationImpl imp=new ChannelContrastApplicationImpl();
//		String startRowKey="00002CANDLC00CZ12222013020900";
//		String   endRowKey="00002CANDLC00CZ63222013021901";
//						  //20005CANSHA00CZ35472013010419 
//		String startRowKey="20005CANSHA00CZ30002013010708";
//		String   endRowKey="20005CANSHA00CZ41002013020419";
//		String startRowKey="20005CANXMN00CZ59882013010200";
//		String   endRowKey="20005CANXMN00CZ59882013010724";
//		String startRowKey="00002HAKWUH2013020114";//海口
//		String   endRowKey="00002HAKWUH2013020114";//
//		String startRowKey="00002HAKWUH2013020100";//海口
//		String   endRowKey="00002HAKWUH2013021400";//00001CANHGH00CA17222013032318
		/*String   endRowKey="00001CANHGH00CA17222013032318";
		String startRowKey="00001CANHGH00CA17222013032318";*/
//		 List dates=getDatesStr("2013-02-07","2013-02-15");
//		 List<Filter> filters=null;
//					String rowKeyHead="00002HAKWUH";//+DateUtils.getPageDateStr(vo.getDepTime(),"yyyyMMdd")
//					filters=getFilter(dates,rowKeyHead, "1");
					List<Filter> filters=new ArrayList();//00002HAKWUH2013021312 and v=0
					///filters.add(new PrefixFilter("00002HAKWUH20130212".getBytes()));
					//SingleColumnValueFilter fliter = new SingleColumnValueFilter("planeinfo".getBytes(),"carrierKey".getBytes(),CompareOp.EQUAL,"CA".getBytes());

					//filters.add(new PrefixFilter("00002HAKWUH201302101".getBytes()));
					///filters.add()LP_LINE_HOUR LP_FLIGHT
						//20005CANPEK00CZ31072013031200//20005CANPEK00CZ31612013030823//20005CANPEK00CZ31112013031400
		/*List<PlaneInfo> list=imp.earchChannelContrast("LP_LINE_HOUR","00002CANPEK2013031400", "00002CANPEK2013031523",null);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			PlaneInfo planeInfo = (PlaneInfo) iterator.next();
			System.out.println(planeInfo.getFlightBatch() );
		}
		System.out.println(list.size());
		*/
					Calendar cal = Calendar.getInstance();
					Date flightDate = DateUtils.StringToDate("yyyyMMddHH", "1999050123");
					
					cal.setTime(flightDate);
					cal.add(Calendar.DAY_OF_MONTH, -1);
					System.out.println(cal.getTime());
					System.out.println("20130722150000".substring(8, 10));
					
					
	}

	
	
}
